{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Dial codes and Dictionaries\n",
    "\n",
    "This notebook contains example code from [*Fluent Python*](http://shop.oreilly.com/product/0636920032519.do), by Luciano Ramalho.\n",
    "\n",
    "Code by Luciano Ramalho, modified by Allen Downey.\n",
    "\n",
    "MIT License: https://opensource.org/licenses/MIT"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Below, we'll show what happens when we create dictionaries using the data in `DIAL_CODES` when that data is sorted in different ways.\n",
    "\n",
    "We'll start by creating the data&mdash;a list of tuples with country codes and country names."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# dial codes of the top 10 most populous countries\n",
    "DIAL_CODES = [\n",
    "        (86, 'China'),\n",
    "        (91, 'India'),\n",
    "        (1, 'United States'),\n",
    "        (62, 'Indonesia'),\n",
    "        (55, 'Brazil'),\n",
    "        (92, 'Pakistan'),\n",
    "        (880, 'Bangladesh'),\n",
    "        (234, 'Nigeria'),\n",
    "        (7, 'Russia'),\n",
    "        (81, 'Japan'),\n",
    "    ]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can create a Python dictionary using the `dict()` function, with `DIAL_CODES` as the argument. Using the `.keys()` method, we can get a list of all of `d1`'s keys."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "d1: dict_keys([880, 1, 86, 55, 7, 234, 91, 92, 62, 81])\n"
     ]
    }
   ],
   "source": [
    "d1 = dict(DIAL_CODES)\n",
    "print('d1:', d1.keys())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice that the keys are *not* in the same order as in `DIAL_CODES`.\n",
    "\n",
    "Let's create two more dictionaties and sort the input data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "d2: dict_keys([880, 1, 91, 86, 81, 55, 234, 7, 92, 62])\n"
     ]
    }
   ],
   "source": [
    "d2 = dict(sorted(DIAL_CODES))\n",
    "print('d2:', d2.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "d3: dict_keys([880, 81, 1, 86, 55, 7, 234, 91, 92, 62])\n"
     ]
    }
   ],
   "source": [
    "d3 = dict(sorted(DIAL_CODES, key=lambda x:x[1]))\n",
    "print('d3:', d3.keys())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Again, we see the keys are not in the same order as in `DIAL_CODES` or as in `d1`.\n",
    "\n",
    "However, the three dictionaries compare equal."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "assert d1 == d2 and d2 == d3"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
